package com.baseweb.webadmin.controller.mobile;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.constants.BaseState;
import com.baseweb.webadmin.core.constants.ChannelType;
import com.baseweb.webadmin.core.constants.OrderStatus;
import com.baseweb.webadmin.core.model.bvo.OrderCommentBvo;
import com.baseweb.webadmin.core.model.bvo.UserForm;
import com.baseweb.webadmin.core.model.pojo.*;
import com.baseweb.webadmin.core.util.CommonUtil;
import com.baseweb.webadmin.service.ContentService;
import com.baseweb.webadmin.service.RoleService;
import com.baseweb.webadmin.service.UserOrderService;
import com.baseweb.webadmin.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping(value = "/mobile/")
public class WebContentController extends BaseController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private UserOrderService userOrderService;

    private String PREFIX = "/mobile/";

    @RequestMapping(value = "/biz_index")
    public String biz_index(@RequestParam(value = "user_id", required = false) Integer user_id,
                            Model model) {

//        List<ByChannel> topList = contentService.selectChannelListByPage(ChannelType.TOP_CHANNEL.getCode(), 0, 0, 6);
//        model.addAttribute("topList", topList);
//
//        User user = userService.selectUserById(user_id);
//        model.addAttribute("user", user);
//
//        List<ByUserBizAuth> authList = userService.selectBizVerifyListByPage(user.getId(), null, null, 0,3);
//        model.addAttribute("authList", authList);
//
//        List<ByUserBizProduct> productList = userService.selectProductListByPage(user.getId(), null, null, 0, 6,null,null,null,null);
//        model.addAttribute("productList", productList);

        List<ByChannel> topList = contentService.selectChannelListByPage(ChannelType.TOP_CHANNEL.getCode(), 0, BaseState.NORMAL.getCode(), 0, 6);
        model.addAttribute("topList", topList);

//        List<ByUserBizProduct> productList = userService.selectProductListByPage(null, product_type, channel_id,(page-1)*pageSize, pageSize,
//                sort, style_type, price, keyword);
//        int total = userService.selectProductListCount(null, product_type, channel_id,sort, style_type, price, keyword);
//
//        model.addAttribute("productList", productList);
//        model.addAttribute("total", total);
//        model.addAttribute("pageSize", pageSize);

        User user = userService.selectUserById(user_id);
        model.addAttribute("user", user);

        ByChannel channel = contentService.findChannel(user.getBizWorkType());
        model.addAttribute("channel", channel);

        List<ByUserBizAuth> authList = userService.selectBizVerifyListByPage(user.getId(), null, null, 0,5, BaseState.NORMAL.getCode());
        model.addAttribute("authList", authList);

        List<ByUserBizProduct> productList = userService.selectProductListByPage(user.getId(), null, null, 0, 6,null,null,null,null, BaseState.NORMAL.getCode());
        model.addAttribute("productList", productList);

        List<ByUserExample> exampleList = userService.selectExampleListByPage(user.getId(), BaseState.NORMAL.getCode(), null, 0, 8);
        model.addAttribute("exampleList", exampleList);


        //开通城市
        Map<String, List<String>> openCitiesMap = new HashMap<String, List<String>>();
        List<ByOpenCity> opencities = userService.selectAllCitiesObjectList(user.getId());
        if (opencities !=null && opencities.size() > 0){
            Set provinceSet = new HashSet();

            for (ByOpenCity current : opencities){
                if (!provinceSet.contains(current.getProvince())){
                    List<String> tmpCities = new ArrayList<String>();
                    openCitiesMap.put(current.getProvince(), tmpCities);
                    provinceSet.add(current.getProvince());
                }
            }
            model.addAttribute("openProvinces", new ArrayList<String>(provinceSet));

            Set citiesSet = new HashSet();
            for (ByOpenCity current : opencities){
                if (!citiesSet.contains(current.getCity())){
                    List<String> tmpCities = openCitiesMap.get(current.getProvince());
                    tmpCities.add(current.getCity());
                    citiesSet.add(current.getCity());
                }
            }
        }
        model.addAttribute("openCitiesMap", openCitiesMap);


        //评论
//        List<ByUserOrderDetail> detailList = userOrderService.selectOrderDetailListByPage(user_id, null, 0, Integer.MAX_VALUE);
//        List<OrderCommentBvo> detailBvoList = new ArrayList<OrderCommentBvo>();
//        if (detailList != null && detailList.size() > 0){
//            for (ByUserOrderDetail current : detailList){
//                OrderCommentBvo bvo = OrderCommentBvo.copyFrom(current, userService.selectUserById(current.getUserId()));
//                detailBvoList.add(bvo);
//            }
//        }

        List<ByUserOrder> commentList = userOrderService.selectOrderListByBizUser(null, user_id, OrderStatus.CONFIRM_ORDER.getCode(), null,0, Integer.MAX_VALUE);

        List<OrderCommentBvo> detailBvoList = new ArrayList<OrderCommentBvo>();
        if (commentList != null && commentList.size() > 0){
            for (ByUserOrder current : commentList){
                OrderCommentBvo bvo = OrderCommentBvo.copyFrom(current, userService.selectUserById(current.getUserId()));
                detailBvoList.add(bvo);
            }
        }
        model.addAttribute("commentList", detailBvoList);


        //档期
//        List<ByUserBizSchedule> scheduleList = userOrderService.selectScheduleListByPage(user_id,null,0,Integer.MAX_VALUE);
//        model.addAttribute("scheduleList",scheduleList);

        Map<String, String> scheduleData = userOrderService.getUserScheduleListView(user_id, 0, 0);

        if (scheduleData != null && scheduleData.keySet().size() > 0) {
            try {
                model.addAttribute("scheduleData", new ObjectMapper().writeValueAsString(scheduleData));
            } catch (IOException e) {
                model.addAttribute("scheduleData", "");
                e.printStackTrace();
            }
        }else{
            model.addAttribute("scheduleData", "");
        }

        //信誉
        String creditStr = CommonUtil.getBizCreditFlag(user.getBizDealCount());
        if (StringUtils.isNotBlank(creditStr)) {
            model.addAttribute("creditData", creditStr);
        }

        return PREFIX + "biz_index";
    }

    @RequestMapping(value = "/faqList")
    public String faqList() {

        return PREFIX + "faq_list";
    }
}
